<?php
Yii::import('zii.widgets.CDetailView');
class DetailView extends CDetailView {
	public $itemTemplate = '<tr><th width=160px class="vtop" nowrap>{label}：</th><td>{value}</td>';
	public $cssFile = false;	
	
	public function init() {
		if(isset($this->htmlOptions['class'])) {
			$this->htmlOptions['class'] .= ' detail';
		} else {
			$this->htmlOptions['class'] = 'detail';
		}
	}
	
	public function run()
	{
		
		$formatter=$this->getFormatter();
		echo CHtml::openTag($this->tagName,$this->htmlOptions);

		$i=0;
		$n=is_array($this->itemCssClass) ? count($this->itemCssClass) : 0;
						
		foreach($this->attributes as $attribute)
		{
			if(is_string($attribute))
			{
				if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$attribute,$matches))
					throw new CException(Yii::t('zii','The attribute must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
				$attribute=array(
					'name'=>$matches[1],
					'type'=>isset($matches[3]) ? $matches[3] : 'text',
				);
				if(isset($matches[5]))
					$attribute['label']=$matches[5];
			}
			
			if(isset($attribute['visible']) && !$attribute['visible'])
				continue;

			$tr=array('{label}'=>'', '{class}'=>$n ? $this->itemCssClass[$i%$n] : '');
			if(isset($attribute['cssClass']))
				$tr['{class}']=$attribute['cssClass'].' '.($n ? $tr['{class}'] : '');

			if(isset($attribute['label']))
				$tr['{label}']=$attribute['label'];
			else if(isset($attribute['name']))
			{
				if($this->data instanceof CModel)
					$tr['{label}']=$this->data->getAttributeLabel($attribute['name']);
				else
					$tr['{label}']=ucwords(trim(strtolower(str_replace(array('-','_','.'),' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $attribute['name'])))));
			}
			
			if(isset($attribute['class'])) {
				$tr['{value}'] = Yii::createComponent(array_merge($attribute, array(
					'model' => $this->data,
				)))->render();
			} else {
				if(!isset($attribute['type']))
					$attribute['type']='text';
				if(isset($attribute['value']))
					$value=$attribute['value'];
				else if(isset($attribute['name']))
					$value=CHtml::value($this->data,$attribute['name']);
				else
					$value=null;
				$tr['{value}']=$value===null ? $this->nullDisplay : $formatter->format($value,$attribute['type']);
			}

			echo strtr(isset($attribute['template']) ? $attribute['template'] : $this->itemTemplate,$tr);
			
			$i++;
															
		}

		echo CHtml::closeTag($this->tagName);
	}
}

class DetailViewAttribute extends CComponent {
	public $template;
	public $value;
	public $name;
	public $label;
	public $classCss;
	public $visible;
	public $type;
	public $model;
}

class ImageAttribute extends DetailViewAttribute {
	public $handle;
	public $htmlOptions;
	public $width = null;
	public $height = null;
	public function render() {
		list($model, $attribute, $htmlOptions) = CActiveFormAdv::resolveArgs(
			$this->model, $this->name
		);
		$url = $model->{$this->handle}->getAccessUrl($attribute);
		$path = $model->{$this->handle}->getAccessPath($attribute);
		$htmlOptions = $this->htmlOptions;
		if(isset($htmlOptions['class'])) {
			$htmlOptions['class'] .= ' img-box';
		} else {
			$htmlOptions['class'] = 'img-box';
		}
//		if($this->width)
//			$htmlOptions['width'] = $this->width;
//		if($this->height)
//			$htmlOptions['height'] = $this->height;
		$html = Yii::app()->img->config('default')->out($model->{$attribute}, $htmlOptions, CUtilHelper::img()->maxSize($path,200,200));
		return $html;
	}
	
}

?>